package com.lengyu.vxminiprogram.vx.miniprogram.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.lengyu.vxminiprogram.common.HttpRequestUtil;
import com.lengyu.vxminiprogram.redis.RedisUtils;
import com.lengyu.vxminiprogram.vx.miniprogram.service.IVxApiService;
import com.lengyu.vxminiprogram.vx.miniprogram.service.config.VxConst;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class VxApiServiceImpl implements IVxApiService {
    private final static Logger logger = LoggerFactory.getLogger(VxApiServiceImpl.class);

    @Autowired
    RedisUtils redisUtils;

    /**
     * 登录凭证校验。通过 wx.login 接口获得临时登录凭证 code 校验登录信息
     * @param code
     * @return
     */
    @Override
    public JSONObject code2Session(String code) {

        String result = HttpRequestUtil.doGet(VxConst.BASEURL+"jscode2session?appid="+VxConst.APPID+
                "&secret="+VxConst.APPKEY+
                "&js_code="+ code+
                "&grant_type=authorization_code","");
        JSONObject resultJson = JSONObject.parseObject(result);
        return resultJson;
    }

    @Override
    public JSONObject sendSubscribeMessage(String title,String detail,String deadLineDate, String openId) {
        String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="+getAccessToken();
        JSONObject msgDemo = new JSONObject();
        msgDemo.put("touser",openId);
        msgDemo.put("template_id",VxConst.MESSAGE_TEMPLATE_ID);
        msgDemo.put("page","/pages/work-order/orderList/index?type=1");


        JSONObject dataJson = new JSONObject();
        JSONObject titleJson = new JSONObject();
        titleJson.put("value",title);
        dataJson.put("thing4",titleJson);
        JSONObject detailJson = new JSONObject();
        detailJson.put("value",detail);
        dataJson.put("thing2",detailJson);
        JSONObject deadLineDateJson = new JSONObject();
        deadLineDateJson.put("value",deadLineDate);
        dataJson.put("time16",deadLineDateJson);
        msgDemo.put("data",dataJson);
        logger.info("调用消息模板参数：" + JSONObject.toJSONString(msgDemo));
        String result = HttpRequestUtil.doPost(url,"",JSONObject.toJSONString(msgDemo));

        logger.info("调用接口消息返回" + result);
        return null;
    }

    @Override
    public String getAccessToken() {
        String token = redisUtils.get("vx_api_token");
        if(StringUtils.isNotBlank(token)){
            logger.info("从redis获取token");
            return token;
        }
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+VxConst.APPID+"&secret="+VxConst.APPKEY;
        String result = HttpRequestUtil.doGet(url,"");
        JSONObject resultJson = JSONObject.parseObject(result);
        logger.info(JSONObject.toJSONString(resultJson));
        if(resultJson.containsKey("access_token")){
            redisUtils.set("vx_api_token",resultJson.getString("access_token"),6500, TimeUnit.SECONDS);
        }
        return resultJson.getString("access_token");
    }
}
